#+_+ coding:utf-8 +_+

import tornado.web
from ds import *
import buffer
import errno
import auth
import storage
import time
from base import BaseHdl 
from storage import news


class PostNewsHdl(BaseHdl):
    """ Handler for post news """
    def post(self):
        uid = self.get_argument("uid","0")
        signature = self.get_argument("signature","")
        author  = self.get_argument("author","")
        title = self.get_argument("title","")
        content = self.get_argument("content","")

        rst = auth.IsValiedUID(uid)
        if not rst:
            rep = self.uidError()
        else:
            ticket = storage.uid.UID.GetTicket(uid)
            rst = auth.CheckSignature(signature,uid,title,content,ticket,author)
            if not rst :
                rep = self.signatureError()
            else:
                id = self.store(author,title,content)
                rep = self.success(id)
        self.write(rep)


    def success(self,newsID):
        msg = dict(news_id=newsID)
        rep = dict(rst="SUCCESS",msg=msg)
        return buffer.JsonEncode(rep)


    def store(self,author,title,content):
        newsID = news.GenerateID()
        news.Add(newsID,author,time.time(),title,content)
        return newsID

        
